package com.book.shelves.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.business.entity.BookSell;
import com.book.business.service.BookSellService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.annotation.Log;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.BookMethod;
import com.book.common.utils.FebsUtil;
import com.book.shelves.entity.BookStore;
import com.book.shelves.entity.ShelvesApply;
import com.book.shelves.service.BookStoreService;
import com.book.shelves.service.ShelvesApplyService;
import com.book.warehouse.entity.WarehouseBook;
import com.book.warehouse.service.WarehouseBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author: Zhang Gaohe
 * @Date: 2021/5/6
 **/

@Controller
public class ShelvesApplyController extends BaseController {
    @Autowired
    ShelvesApplyService shelvesApplyService;
    @Autowired
    WarehouseBookService warehouseBookService;
    @Autowired
    BookStoreService bookStoreService;
    @Autowired
    BookSellService bookSellService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "shelves/shelvesApply")
    public String shelvesApplyIndex() {
        return FebsUtil.view("shelves/shelvesApply/shelvesApplyList");
    }

    @Log("架存-上架申请")
    @GetMapping("shelvesApply/list")
    @ResponseBody
    @RequiresPermissions("shelvesApply:view")
    public FebsResponse shelvesApplyIPage(QueryRequest request, WarehouseBook warehouseBook) {
        Map<String, Object> dataTable = getDataTable(this.warehouseBookService.WarehouseBookApplyPage(request, warehouseBook, "0"));
        return new FebsResponse().success().data(dataTable);
    }

    @Log("架存-上架审批")
    @PostMapping("shelvesApply/yes/{ids}")
    @ResponseBody
    @RequiresPermissions("shelvesApply:yes")
    @ControllerEndpoint(operation = "审批成功", exceptionMessage = "审批失败")
    public FebsResponse shelvesApplyYes(@PathVariable String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        for (int i = 0; i < list.size(); i++) {
            WarehouseBook warehouseBook = warehouseBookService.getOne(new QueryWrapper<WarehouseBook>().lambda()
                    .in(WarehouseBook::getBookid, list.get(i)));
            BookStore one = bookStoreService.getOne(new QueryWrapper<BookStore>().lambda()
                    .eq(BookStore::getBookname, warehouseBook.getBookname()).eq(BookStore::getFlag, "0"));
            warehouseBook.setPutaway("1");
            warehouseBookService.updateWarehouseBook(warehouseBook);
            BookSell bookSell = new BookSell()
                    .setBookname(warehouseBook.getBookname())
                    .setNumber(warehouseBook.getNumber())
                    .setCart(0)
                    .setLove(0)
                    .setBookmoney(warehouseBook.getBookmoney())
                    .setMessage(BookMethod.message(warehouseBook.getType()))
                    .setPubid(warehouseBook.getPurchasefactory())
                    .setType(warehouseBook.getType());
            bookSellService.addBookSell(bookSell);
            // 避免手动添加
            BookStore bookStore = new BookStore()
                    .setBookname(warehouseBook.getBookname())
                    .setNum(warehouseBook.getNumber())
                    .setBookmoney(warehouseBook.getBookmoney())
                    .setType(warehouseBook.getType())
                    .setPubid(warehouseBook.getPurchasefactory())
                    .setWarningnumber(100);
            if (warehouseBook.getNumber() >= 100) {
                bookStore.setFlag("0");
            } else {
                bookStore.setFlag("2");
            }
            if (one == null) {
                bookStoreService.addBookStore(bookStore);
            } else {
                bookStoreService.removeById(one.getId());
                bookStoreService.addBookStore(bookStore);
            }
        }
        shelvesApplyService.remove(new QueryWrapper<ShelvesApply>().lambda().in(ShelvesApply::getBookid, list));
        return new FebsResponse().success();
    }

    @Log("架存-上架驳回")
    @PostMapping("shelvesApply/no/{ids}")
    @ResponseBody
    @RequiresPermissions("shelvesApply:no")
    @ControllerEndpoint(operation = "驳回成功", exceptionMessage = "驳回失败")
    public FebsResponse shelvesApplyNo(@PathVariable String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        shelvesApplyService.remove(new QueryWrapper<ShelvesApply>().lambda().in(ShelvesApply::getBookid, list));
        return new FebsResponse().success();
    }
}
